package com.xiaoxu.intranetweb.mybatisplusSzappdb.Dic;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.dao.UserInfoDao;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.CCompanys;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.CFinaceEnter;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.COrderDetail;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.CWeightSheets;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.mapper.CWeightSheetsMapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ICCompanysService;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ICFinaceEnterService;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ICOrderDetailService;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.ICWeightSheetsService;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.vo.GetBalanceEnquiryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class BalanceDic {
    @Autowired
    private CWeightSheetsMapper cWeightSheetsMapper;
    @Autowired
    private ICOrderDetailService iCOrderDetailService;
    @Autowired
    private UserInfoDao userInfoDao;
    @Autowired
    private ICFinaceEnterService iCFinaceEnterService;
    @Autowired
    private ICWeightSheetsService iCWeightSheetsService;
    @Autowired
    private ICCompanysService iCCompanysService;

    //    区域类型
    public Map<String, String> balanceDic(String startTime, String endTime, String co, String chi, COrderDetail cOrderDetail, String plantId) throws Exception {
        Map<String, String> map = new HashMap<>();
        String ordetailSubId = cOrderDetail.getOrdetailSubId();
        QueryWrapper<CCompanys> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", ordetailSubId);
        CCompanys cCompanys = iCCompanysService.getOne(queryWrapper);
        GetBalanceEnquiryVo getBalanceEnquiryVo1 = cWeightSheetsMapper.getBalanceEnquiry(startTime, endTime, co, chi, plantId);
        GetBalanceEnquiryVo balanceEnd = cWeightSheetsMapper.getBalanceEnd(endTime, cOrderDetail.getOrdetailCo(), cOrderDetail.getOrdetailChico(),cCompanys.getCpyCo(), cCompanys.getCpyChico(),plantId);
        GetBalanceEnquiryVo balanceEnd2 = cWeightSheetsMapper.getBalanceEnd2(endTime, cOrderDetail.getOrdetailCo(), cOrderDetail.getOrdetailChico(), String.valueOf(ordetailSubId),plantId);
        //时间点止余额（元）
        if (balanceEnd == null && balanceEnd2 != null) {
            map.put("balanceEnd", String.valueOf(balanceEnd2.getBalanceEnd()));
        }
        if (balanceEnd2 == null && balanceEnd != null) {
            map.put("balanceEnd", String.valueOf(balanceEnd.getBalanceEnd()));
        }
        if (balanceEnd != null && balanceEnd2 != null) {
            map.put("balanceEnd", String.valueOf(balanceEnd2.getBalanceEnd() - balanceEnd.getBalanceEnd()));
        }
        //时间点期间扣款（元）
        if (getBalanceEnquiryVo1!=null){
            map.put("balanceCons", String.valueOf(getBalanceEnquiryVo1.getBalanceConsume()));
        }else {
            map.put("balanceCons", String.valueOf(0.0));
        }
        return map;
    }

    //获取时间点余额方法
    public BigDecimal balanceEnd(String token, String co, String chi, String endTime) throws Exception {
        BigDecimal balanceEnd;
        //获取时间点止余额的充值金额
        QueryWrapper<CFinaceEnter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("DATA_FROM", userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId());
        queryWrapper.eq(co != null && !co.equals(""), "FINANCE_ENTER_CO", co);
        queryWrapper.eq(chi != null && !chi.equals(""), "FINANCE_ENTER_CHI_CO", chi);
        queryWrapper.le(endTime != null && !endTime.equals(""), "FINANCE_ENTER_TIME", endTime);//小于等于
        //把所有查出
        List<CFinaceEnter> list = iCFinaceEnterService.list(queryWrapper);
        BigDecimal FinaceEnterBalanceEnd = BigDecimal.ZERO;
        for (CFinaceEnter cFinaceEnter : list) {
            BigDecimal financeEnterNum = cFinaceEnter.getFinanceEnterNum();
            FinaceEnterBalanceEnd = FinaceEnterBalanceEnd.add(financeEnterNum);
        }

        //获取时间点止余额的消费金额
        QueryWrapper<CWeightSheets> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("DATA_FROM", userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId());
        queryWrapper1.eq(co != null && !co.equals(""), "WEISHT_CO_SUPPLY", co);
        queryWrapper1.eq(chi != null && !chi.equals(""), "WEISHT_CO_SUPPLY_CHI", chi);
        queryWrapper1.le(endTime != null && !endTime.equals(""), "WEISHT_FULL_TM", endTime);//小于等于
        List<CWeightSheets> list1 = iCWeightSheetsService.list(queryWrapper1);
        BigDecimal CWeightSheetsBalanceEnd = BigDecimal.ZERO;
        if (list1 != null) {
            for (CWeightSheets cWeightSheets : list1) {
                BigDecimal weishtNum = cWeightSheets.getWeishtMoney();
                CWeightSheetsBalanceEnd = CWeightSheetsBalanceEnd.add(weishtNum);
            }
        }
        QueryWrapper<CWeightSheets> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("DATA_FROM", userInfoDao.getUserFactory(StpUtil.getLoginIdByToken(token).toString()).getId());
        queryWrapper2.eq(co != null && !co.equals(""), "WEISHT_CO", co);
        queryWrapper2.eq(chi != null && !chi.equals(""), "WEISHT_CHI_CO", chi);
        queryWrapper2.le(endTime != null && !endTime.equals(""), "WEISHT_FULL_TM", endTime);//小于等于
        List<CWeightSheets> list2 = iCWeightSheetsService.list(queryWrapper2);
        BigDecimal CWeightSheetsBalanceEnd1 = BigDecimal.ZERO;
        if (list2 != null) {
            for (CWeightSheets cWeightSheets : list2) {
                BigDecimal weishtNum = cWeightSheets.getWeishtMoney();
                CWeightSheetsBalanceEnd1 = CWeightSheetsBalanceEnd1.add(weishtNum);
            }
        }

        balanceEnd = FinaceEnterBalanceEnd.add(CWeightSheetsBalanceEnd).add(CWeightSheetsBalanceEnd1);
//        System.out.println("213135654");
//        System.out.println(balanceEnd);
        return balanceEnd;
    }

}
